<?php
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

/**
 * 后台首页控制器
 */
class ShareController extends AdminBaseController{
	/**
	 * 首页
	 */
	public function index(){

		$keyword = I('keyword');

		if(!empty($keyword)){
			$map['name'] = array('like','%'.$keyword.'%');
		}

		$Data = M('share'); // 实例化Data数据对象  date 是你的表名
		$count = $Data->where($map)->count();// 查询满足要求的总记录数 $map表示查询条件
		$Page = new \Think\Page($count);// 实例化分页类 传入总记录数
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$show = $Page->show();// 分页显示输出
		// 进行分页数据查询
		$list = $Data->where($map)->order('id')->limit($Page->firstRow.','.$Page->listRows)->select(); // $Page->firstRow 起始条数 $Page->listRows 获取多少条

		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display(); // 输出模板
	}


	/**
	 * 编辑
	 */
	public function editor(){

		if(IS_POST){

			$post = I("post.");

			//图片上传
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     3145728 ;// 设置附件上传大小
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->rootPath  =      './Upload/share/'; // 设置附件上传根目录
			$upload->subName  = array('date','Ymd');

			//插入新数据
			if(empty($post['id'])){

				// 上传单个文件
				$info   =   $upload->uploadOne($_FILES['photo']);

				if(!$info) {// 上传错误提示错误信息
					$this->error($upload->getError());
				}else{// 上传成功 获取上传文件信息

					$data = array(
						"pic"=> $upload->rootPath.$info['savepath'].$info['savename'],
						"name"=>$post['name'],
						"describe"=>$post['describe'],
						"url"=>$post['url'],
					);

					M('share')->data($data)->add();

					$this->success('操作完成',U('Admin/Share/index'),3);
				}

			}else{

				if(empty($_FILES['photo']['type'])){

					$data = array(
						"name"=>$post['name'],
						"describe"=>$post['describe'],
						"url"=>$post['url'],
					);

				}else{

					// 上传单个文件
					$info   =   $upload->uploadOne($_FILES['photo']);

					if(!$info) {// 上传错误提示错误信息
						$this->error($upload->getError());
					}else{// 上传成功 获取上传文件信息

						$id = $post['id'];

						$where['id'] = array("eq",$id);

						$items = M('share')->where($where)->find();

						unlink($items['pic']);

						$data = array(
							"pic"=>$upload->rootPath.$info['savepath'].$info['savename'],
							"name"=>$post['name'],
							"describe"=>$post['describe'],
							"url"=>$post['url'],
						);

					}

				}

				M('share')->where('id='.$post['id'])->save($data);

				$this->success('操作完成',U('Admin/Share/index'),3);

			}

		}else{

			$id = I("get.id");

			$where['id'] = array("eq",$id);

			$items = M('share')->where($where)->find();

			$items['pic'] = substr($items['pic'],1);

			$this->assign($items);
			$this->display();
		}
	}

	/**
	 * 删除分享
	 */
	function delete(){

		$id = I('get.id');

		$map['id'] = array("eq",$id);

		$items = M('share')->where($map)->find();

		unlink($items['pic']);

		M('share')->where($map)->delete();

		$this->success('操作完成',U('Admin/Share/index'),3);
	}

}
